window.onload = function() {
	let register = document.querySelector("button[class='login']");
	register.addEventListener("click", () => {
		let username = document.querySelector(".username").value;
		console.log(username)
		let password = document.querySelector(".password").value;
		const usernameReg = new RegExp(/^[a-zA-Z0-9_]{3,12}$/, 'i');
		const passwordReg = new RegExp(/^[0-9]{6,12}$/, 'i');
		if (username == '') {
			alert("用户名不能为空！");
			window.location.href = 'login.html'
		} else {
			if (password == '') {
				alert("密码不能为空！");
				window.location.href = 'login.html'
			} else {
				if (!usernameReg.test(username)) {
					alert("用户名或密码错误！");
					window.location.href = 'login.html';
				} else {
					if (!passwordReg.test(password)) {
						alert("用户名或密码错误！");
						window.location.href = 'login.html'
					}
				}
			}
		}
		if (usernameReg.test(username) && passwordReg.test(password)) {
			(function(){
				ajax({
					method:"POST",
					url:BASE_URL+"/api_user",
					params:"username="+username+"&password="+password+"&status=login",
					type:"JSON",
					callback:function(data){
						console.log(data)
						alert(data.message);
						if(data.message=='登录成功'){
							// 长期存储(除非手动清除)
							localStorage.setItem("user_id",data.user_id);
							localStorage.setItem("user_name",data.username);
							//(临时存储，页面关闭失效)
							// sessionStorage.setItem("user_id",data.user_id);
							window.location.href='index.html';
						}
						else{
							window.location.href='login.html';
						}
					}
				})
			})();
		}
	})
}
